package com.example.peibei.ui.fragment;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.dingtao.common.bean.GetOrder;
import com.dingtao.common.core.DataCall;
import com.dingtao.common.core.WDFragment;
import com.dingtao.common.core.exception.ApiException;
import com.dingtao.common.util.SPUtils;
import com.example.peibei.R;
import com.example.peibei.base.SpConstant;
import com.example.peibei.service.presenter.GetOrderPresenter;
import com.example.peibei.ui.activity.OrderDetailActivity;
import com.example.peibei.ui.activity.SendOrderDetailActivity;
import com.example.peibei.ui.adapter.GetOrderAdapter;
import com.example.peibei.ui.adapter.SendOrderAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MySendOrderListFragment extends WDFragment {
    @BindView(R.id.rv_get_order)
    RecyclerView rv_get_order;

    private int status;
    List<GetOrder.RecordsDTO> list = new ArrayList<>();
    private SendOrderAdapter sendOrderAdapter;
    private GetOrderPresenter getOrderPresenter;
    private SPUtils spUtils;
    private String token;

    public MySendOrderListFragment(int status) {
        this.status = status;
    }

    @Override
    public String getPageName() {
        return "订单fragment";
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_my_get_order;
    }

    @Override
    protected void initView() {
        spUtils = new SPUtils(getActivity(), SpConstant.USER_TOKEN);
        token = spUtils.getString(SpConstant.USER_TOKEN);
        rv_get_order.setLayoutManager(new LinearLayoutManager(getActivity()));
        sendOrderAdapter = new SendOrderAdapter(list);
        rv_get_order.setAdapter(sendOrderAdapter);
        getOrderPresenter = new GetOrderPresenter(new OrderCall());
        getOrderPresenter.reqeust(true, "Bearer " + token, status);
        sendOrderAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                List<GetOrder.RecordsDTO> data = (List<GetOrder.RecordsDTO>) adapter.getData();
                GetOrder.RecordsDTO order = data.get(position);
                String consumeOrderId = order.getConsumeOrderId();
                Bundle bundle = new Bundle();
                bundle.putString("consumeOrderId",consumeOrderId);
                bundle.putString("userId",order.getMid());
                bundle.putString("nickName",order.getNickName());
                intent(SendOrderDetailActivity.class,bundle);
            }
        });
    }

    class OrderCall implements DataCall<GetOrder> {

        @Override
        public void success(GetOrder data, Object... args) {
            //添加列表并刷新
            if (getOrderPresenter.getPage() == 1) {
                list.clear();
            }
            sendOrderAdapter.setNewInstance(data.getRecords());
            sendOrderAdapter.notifyDataSetChanged();
        }

        @Override
        public void fail(ApiException e, Object... args) {
        }
    }
}
